/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cliente.java
 *
 * Created on 22/04/2011, 01:17:44
 */

package tp1.views;

import javax.swing.JButton;
import javax.swing.JLabel;
import tp1.controllers.Controller;
import java.sql.SQLException;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import tp1.dao.PlanoDao;

/**
 *
 * @author tulio
 */
public class PlanoPanel extends ViewAbstract {

    /** Creates new form Cliente */
    public PlanoPanel() {
        initComponents();
        initGrid();
        initToolbar();
    }

    private void initToolbar(){
        Controller controllerPlano = Controller.factory(Controller.CONTROLLER_PLANO, this);

        controllerPlano.addEvent(buttonSalvar, Controller.EVENTO_SALVAR_PLANO);
        controllerPlano.addEvent(buttonNovo, Controller.EVENTO_NOVO_PLANO);
        controllerPlano.addEvent(buttonEditar, Controller.EVENTO_EDITAR_PLANO);
        controllerPlano.addEvent(buttonApagar, Controller.EVENTO_APAGAR_PLANO);
        controllerPlano.addEvent(buttonBuscar, Controller.EVENTO_BUSCAR_PLANO);

        buttonSalvar.addActionListener(controllerPlano);
        buttonNovo.addActionListener(controllerPlano);
        buttonApagar.addActionListener(controllerPlano);
        buttonEditar.addActionListener(controllerPlano);
        buttonBuscar.addActionListener(controllerPlano);
    }

    public void updateGridData() throws SQLException{
        Object[][] data = PlanoDao.listAsVector();
        this.setGridData(data);
    }

    @Override
    public void reloadDefaults() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setGridData(Object[][] data) {
        DefaultTableModel model = (DefaultTableModel) gridPlano.getModel();
        model.setDataVector(data, new String [] {
            "Código", "Descrição", "Valor Mensal", "Valor da Chamada", "Franquia"
        });
    }

    private void initGrid(){
        try {
            /**
             * Define os dados do grid
             */
            updateGridData();

            /**
             * Listener para a seleção de linhas no grid - captura valores e define-os no formulário
             */
            gridPlano.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    if(gridPlano.getModel().getRowCount() > gridPlano.getSelectedRow() && gridPlano.getSelectedRow() != -1){
                        Object codigo = gridPlano.getModel().getValueAt(gridPlano.getSelectedRow(), 0);
                        Object descricao = gridPlano.getModel().getValueAt(gridPlano.getSelectedRow(), 1);
                        Object mensal = gridPlano.getModel().getValueAt(gridPlano.getSelectedRow(), 2);
                        Object chamada = gridPlano.getModel().getValueAt(gridPlano.getSelectedRow(), 3);
                        Object franquia = gridPlano.getModel().getValueAt(gridPlano.getSelectedRow(), 4);
                        fieldCodigo.setEditable(false);
                        fieldCodigo.setText(codigo.toString());
                        fieldDescricao.setText(descricao.toString());
                        fieldValorMensal.setText(mensal.toString());
                        fieldValorChamada.setText(chamada.toString());
                        fieldFranquia.setText(franquia.toString());
                    }
                }
            });
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabs = new javax.swing.JTabbedPane();
        tabGrid = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        gridPlano = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        buttonNovo = new javax.swing.JButton();
        buttonEditar = new javax.swing.JButton();
        buttonApagar = new javax.swing.JButton();
        buttonBuscar = new javax.swing.JButton();
        tabForm = new javax.swing.JPanel();
        labelCodigo = new javax.swing.JLabel();
        labelDescricao = new javax.swing.JLabel();
        labelFranquia = new javax.swing.JLabel();
        buttonSalvar = new javax.swing.JButton();
        fieldDescricao = new javax.swing.JTextField();
        fieldFranquia = new javax.swing.JTextField();
        fieldCodigo = new javax.swing.JTextField();
        fieldValorChamada = new javax.swing.JTextField();
        fieldValorMensal = new javax.swing.JTextField();
        labelValorMensal = new javax.swing.JLabel();
        labelValorChamada = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(971, 447));

        gridPlano.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Descrição", "Valor Mensal", "Valor (por segundo)", "Franquia (minutos)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Float.class, java.lang.Float.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(gridPlano);

        jToolBar1.setRollover(true);

        buttonNovo.setText("Novo");
        buttonNovo.setFocusable(false);
        buttonNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonNovo);

        buttonEditar.setText("Editar");
        buttonEditar.setFocusable(false);
        buttonEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonEditar);

        buttonApagar.setText("Apagar");
        buttonApagar.setFocusable(false);
        buttonApagar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonApagar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonApagar);

        buttonBuscar.setText("Buscar");
        buttonBuscar.setFocusable(false);
        buttonBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(buttonBuscar);

        javax.swing.GroupLayout tabGridLayout = new javax.swing.GroupLayout(tabGrid);
        tabGrid.setLayout(tabGridLayout);
        tabGridLayout.setHorizontalGroup(
            tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabGridLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE))
                .addContainerGap())
        );
        tabGridLayout.setVerticalGroup(
            tabGridLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabGridLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabs.addTab("Dados", tabGrid);

        labelCodigo.setText("Código");

        labelDescricao.setText("Descrição");

        labelFranquia.setText("Franquia (minutos)");

        buttonSalvar.setText("Salvar");

        fieldValorMensal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fieldValorMensalActionPerformed(evt);
            }
        });

        labelValorMensal.setText("Valor Mensal");

        labelValorChamada.setText("Valor (por segundo)");

        javax.swing.GroupLayout tabFormLayout = new javax.swing.GroupLayout(tabForm);
        tabForm.setLayout(tabFormLayout);
        tabFormLayout.setHorizontalGroup(
            tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabFormLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelFranquia)
                    .addComponent(labelCodigo)
                    .addComponent(labelDescricao)
                    .addComponent(labelValorChamada)
                    .addComponent(labelValorMensal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonSalvar)
                    .addComponent(fieldValorMensal, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fieldValorChamada, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(fieldCodigo, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(fieldDescricao, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(fieldFranquia, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)))
                .addGap(109, 109, 109))
        );
        tabFormLayout.setVerticalGroup(
            tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabFormLayout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCodigo)
                    .addComponent(fieldCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelDescricao))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFranquia)
                    .addComponent(fieldFranquia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldValorChamada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelValorChamada))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldValorMensal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelValorMensal))
                .addGap(18, 18, 18)
                .addComponent(buttonSalvar)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        tabs.addTab("Formulário", tabForm);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(tabs, javax.swing.GroupLayout.PREFERRED_SIZE, 694, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(242, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(tabs, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(43, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void fieldValorMensalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fieldValorMensalActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_fieldValorMensalActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonApagar;
    private javax.swing.JButton buttonBuscar;
    private javax.swing.JButton buttonEditar;
    private javax.swing.JButton buttonNovo;
    private javax.swing.JButton buttonSalvar;
    private javax.swing.JTextField fieldCodigo;
    private javax.swing.JTextField fieldDescricao;
    private javax.swing.JTextField fieldFranquia;
    private javax.swing.JTextField fieldValorChamada;
    private javax.swing.JTextField fieldValorMensal;
    private javax.swing.JTable gridPlano;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel labelCodigo;
    private javax.swing.JLabel labelDescricao;
    private javax.swing.JLabel labelFranquia;
    private javax.swing.JLabel labelValorChamada;
    private javax.swing.JLabel labelValorMensal;
    private javax.swing.JPanel tabForm;
    private javax.swing.JPanel tabGrid;
    private javax.swing.JTabbedPane tabs;
    // End of variables declaration//GEN-END:variables

    public JButton getButtonApagar() {
        return buttonApagar;
    }

    public void setButtonApagar(JButton buttonApagar) {
        this.buttonApagar = buttonApagar;
    }

    public JButton getButtonBuscar() {
        return buttonBuscar;
    }

    public void setButtonBuscar(JButton buttonBuscar) {
        this.buttonBuscar = buttonBuscar;
    }

    public JButton getButtonEditar() {
        return buttonEditar;
    }

    public void setButtonEditar(JButton buttonEditar) {
        this.buttonEditar = buttonEditar;
    }

    public JButton getButtonNovo() {
        return buttonNovo;
    }

    public void setButtonNovo(JButton buttonNovo) {
        this.buttonNovo = buttonNovo;
    }

    public JButton getButtonSalvar() {
        return buttonSalvar;
    }

    public void setButtonSalvar(JButton buttonSalvar) {
        this.buttonSalvar = buttonSalvar;
    }

    public JTextField getFieldCodigo() {
        return fieldCodigo;
    }

    public void setFieldCodigo(JTextField fieldCodigo) {
        this.fieldCodigo = fieldCodigo;
    }

    public JTextField getFieldDescricao() {
        return fieldDescricao;
    }

    public void setFieldDescricao(JTextField fieldDescricao) {
        this.fieldDescricao = fieldDescricao;
    }

    public JTextField getFieldFranquia() {
        return fieldFranquia;
    }

    public void setFieldFranquia(JTextField fieldFranquia) {
        this.fieldFranquia = fieldFranquia;
    }

    public JTextField getFieldValorChamada() {
        return fieldValorChamada;
    }

    public void setFieldValorChamada(JTextField fieldValorChamada) {
        this.fieldValorChamada = fieldValorChamada;
    }

    public JTextField getFieldValorMensal() {
        return fieldValorMensal;
    }

    public void setFieldValorMensal(JTextField fieldValorMensal) {
        this.fieldValorMensal = fieldValorMensal;
    }

    public JTable getGridPlano() {
        return gridPlano;
    }

    public void setGridPlano(JTable gridPlano) {
        this.gridPlano = gridPlano;
    }

    public JLabel getLabelCodigo() {
        return labelCodigo;
    }

    public void setLabelCodigo(JLabel labelCodigo) {
        this.labelCodigo = labelCodigo;
    }

    public JLabel getLabelDescricao() {
        return labelDescricao;
    }

    public void setLabelDescricao(JLabel labelDescricao) {
        this.labelDescricao = labelDescricao;
    }

    public JLabel getLabelFranquia() {
        return labelFranquia;
    }

    public void setLabelFranquia(JLabel labelFranquia) {
        this.labelFranquia = labelFranquia;
    }

    public JLabel getLabelValorChamada() {
        return labelValorChamada;
    }

    public void setLabelValorChamada(JLabel labelValorChamada) {
        this.labelValorChamada = labelValorChamada;
    }

    public JLabel getLabelValorMensal() {
        return labelValorMensal;
    }

    public void setLabelValorMensal(JLabel labelValorMensal) {
        this.labelValorMensal = labelValorMensal;
    }

    public JPanel getTabForm() {
        return tabForm;
    }

    public void setTabForm(JPanel tabForm) {
        this.tabForm = tabForm;
    }

    public JPanel getTabGrid() {
        return tabGrid;
    }

    public void setTabGrid(JPanel tabGrid) {
        this.tabGrid = tabGrid;
    }

    public JTabbedPane getTabs() {
        return tabs;
    }

    public void setTabs(JTabbedPane tabs) {
        this.tabs = tabs;
    }
    
}
